﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using UniMana.Utils.Data;
using UniMana.Data;

namespace UniMana.Win.Systems
{
    public partial class FrmCreateGroup : DevExpress.XtraEditors.XtraForm
    {
        System_GroupInfo m_sgi;
        public delegate void PassData(System_GroupInfo sgi);
        public PassData passData;

        DataTable m_dtFunctionSet;
        DataTable m_dtFunctionNotSet;
        DataTable m_dtUserGroup;
        /// <summary>
        /// m_dtSys_User_Rule
        /// </summary>
        DataTable m_dtSys_User_Rule;
        public FrmCreateGroup(System_GroupInfo _sgi=null)
        {
            InitializeComponent();
            LoadDataNotSet();
            if (_sgi != null)
            {
                m_sgi = _sgi;
                txtGroupName.Text = m_sgi.Properties.Name;
                meoDescription.Text = m_sgi.Properties.Description;
                //grcSystem_User.DataSource = System_UserData.GetUserByGroup(m_sgi.Properties.System_GroupKey);
                LoadData();
                
            }
            else 
            {
                m_sgi = new System_GroupInfo("");

                //tltFunctionNotSet.DataSource = m_dtFunctionNotSet;
                //tltFunctionNotSet.Columns[0].SortOrder = SortOrder.Ascending;
            }

            

            if (m_dtFunctionSet != null && m_dtFunctionSet.Rows.Count > 0)
            {
                foreach (DataRow dr in m_dtFunctionSet.Rows)
                {
                    DevExpress.XtraTreeList.Nodes.TreeListNode node = tltFunctionSet.FindNodeByFieldValue("RuleID", dr["RuleID"]);
                    if (node.Nodes.Count==0)
                    {
                        DevExpress.XtraTreeList.Nodes.TreeListNode nodeNot = tltFunctionNotSet.FindNodeByFieldValue("RuleID", dr["RuleID"]);
                        //if (nodeNot.Nodes.Count)
                        //{
                            RemoveNode(tltFunctionNotSet, nodeNot);
                            //tltFunctionNotSet.Nodes.Remove(nodeNot);
                        //}
                    }
                }
            //    foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in tltFunctionNotSet.Nodes)
            //    {
            //        if (node.Nodes.Count == 0)
            //        {
            //            if (tltFunctionSet.FindNodeByFieldValue("RuleID", node["RuleID"]) != null)
            //            {
            //                RemoveNode(tltFunctionNotSet, node);
            //            }
            //        }
            //    }
            }
            
        }
        /// <summary>
        /// 
        /// </summary>
        public void LoadRuleByGroup()
        {
            if (m_dtSys_User_Rule == null) m_dtSys_User_Rule = new DataTable();
            if (m_dtSys_User_Rule.Columns.Count == 0)
            {
                m_dtSys_User_Rule.Columns.Add("System_UserKey");
                m_dtSys_User_Rule.Columns.Add("System_RuleKey");

                m_dtSys_User_Rule.Columns.Add("RuleID");
                m_dtSys_User_Rule.Columns.Add("RuleName");
                m_dtSys_User_Rule.Columns.Add("RuleNameVi");
                m_dtSys_User_Rule.Columns.Add("ParentID");
                m_dtSys_User_Rule.Columns.Add("Level");
                m_dtSys_User_Rule.Columns.Add("AllowAccess", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowAdd", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowDelete", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowEdit", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowExport", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowImport", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowPrint", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowSave", System.Type.GetType("System.Boolean"));
            }

            DataTable dtGr = System_RuleData.RuleByGroup(new DBMain(), m_sgi.Properties.System_GroupKey ).ToDataTable();

            foreach (DataRow row in dtGr.Rows)
            {
                m_dtSys_User_Rule.Rows.Add(new object[] { 
                        "", 
                        row["System_RuleKey"],
                        row["RuleID"], 
                        row["RuleName"],
                        row["RuleNameVi"], 
                        row["ParentID"], 
                        row["Level"], 
                       
                               row["AllowAccess"],
                               row["AllowAdd"],
                             row["AllowDelete"],
                             row["AllowEdit"],
                             row["AllowExport"],
                           row["AllowImport"],
                            row["AllowPrint"],
                          row["AllowSave"] });
            }
        }
        private void LoadData()
        {
            //Lấy danh sách 
            m_dtUserGroup = System_UserData.GetUserByGroup(m_sgi.Properties.System_GroupKey).ToDataTable();
            grcSystem_User.DataSource = m_dtUserGroup;

            m_dtFunctionSet = System_RuleData.RuleOfGroup(new DBMain(), m_sgi.Properties.System_GroupKey).ToDataTable();
            tltFunctionSet.DataSource = m_dtFunctionSet;
            tltFunctionSet.Columns[0].SortOrder = SortOrder.Ascending;
            m_dtFunctionSet.AcceptChanges();
            
            
        }
        private void LoadDataNotSet()
        {
            m_dtFunctionNotSet = new DataTable();
            m_dtFunctionNotSet.Columns.Add("RuleID");
            m_dtFunctionNotSet.Columns.Add("RuleName");
            m_dtFunctionNotSet.Columns.Add("RuleNameVi");
            m_dtFunctionNotSet.Columns.Add("Description");
            m_dtFunctionNotSet.Columns.Add("Level");
            m_dtFunctionNotSet.Columns.Add("ParentID");
            m_dtFunctionNotSet.Columns.Add("Owner");

            foreach (DataRow dr in SysGlobalVar.g_dtContainControl.Rows)
            {
                m_dtFunctionNotSet.Rows.Add(
                    dr["RuleID"],
                dr["RuleID"].ToString().Substring(3),
                dr["RuleNameVi"],
                "",
                dr["Level"],
                dr["ParentID"],
                "");

            }
            
            tltFunctionNotSet.DataSource = m_dtFunctionNotSet;
            tltFunctionNotSet.Columns[0].SortOrder = SortOrder.Ascending;
            m_dtFunctionNotSet.AcceptChanges();
            
        }

        private void btnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (txtGroupName.Text != "")
            {
                if (m_sgi == null)
                {
                    m_sgi = new System_GroupInfo(txtGroupName.Text.Trim());
                }
                else
                {
                    m_sgi.Properties.Name = txtGroupName.Text.Trim();
                }

                m_sgi.Properties.Description = meoDescription.Text;
                m_sgi.Save();

                passData(m_sgi);

                Close();
            }
            else
            {
                MessageBox.Show("Tên nhóm không được rỗng");
            }

            SaveGroupRule();
            
        }

        private void SaveGroupRule()
        {
            if (m_dtFunctionSet != null)
            {
            #region Delete
            //System_Group_RuleInfo sgri = new System_Group_RuleInfo(m_sgi.Properties.System_GroupKey, Guid.Parse(node["System_RuleKey"].ToString()));
            #region Xóa Rule khỏi User thuộc nhóm
            
                if (m_dtFunctionSet.GetChanges(DataRowState.Deleted) != null)
                {
                    foreach (DataRow drRule in m_dtFunctionSet.GetChanges(DataRowState.Deleted).Rows)
                    {
                        System_RuleInfo sri = new System_RuleInfo(drRule["RuleID", DataRowVersion.Original].ToString());
                        if (m_dtUserGroup != null)
                        {
                            foreach (DataRow dr in m_dtUserGroup.Rows)
                            {
                                System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(dr["System_UserKey"].ToString()), sri.Properties.System_RuleKey);
                                suri.Delete();
                                suri.Save();
                            }
                        }

                        System_Group_RuleInfo sgri = new System_Group_RuleInfo(m_sgi.Properties.System_GroupKey, sri.Properties.System_RuleKey);
                        sgri.Delete();
                        sgri.Save();
                    }
                }
           

            #endregion
            #endregion
            #region Modidfied
            if(m_dtFunctionSet.GetChanges(DataRowState.Modified)!=null)
            {
                foreach (DataRow drRule in m_dtFunctionSet.GetChanges(DataRowState.Modified).Rows)
                {

                    #region Lưu Rule mới
                    System_RuleInfo sri = new System_RuleInfo(drRule["RuleID"].ToString());
                    sri.Properties.RuleName = drRule["RuleID"].ToString().Substring(3);
                    sri.Properties.RuleNameVi = drRule["RuleNameVi"].ToString();
                    sri.Properties.Level = System.Convert.ToInt16(drRule["Level"]);
                    sri.Properties.ParentID = drRule["ParentID"].ToString();
                    sri.Properties.Description = "";
                    sri.Properties.Owner = "Quản lý đào tạo";
                    sri.Save();
                    #endregion
                    #region Lưu Rule vào nhóm
                    System_Group_RuleInfo sgri = new System_Group_RuleInfo(m_sgi.Properties.System_GroupKey, sri.Properties.System_RuleKey);
                    sgri.Save();
                    #endregion
                    #region Lưu thông rule mới cho user
                    if (m_dtUserGroup != null)
                    {
                        foreach (DataRow dr in m_dtUserGroup.Rows)
                        {
                            System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(dr["System_UserKey"].ToString()), sgri.Properties.System_RuleKey);
                            suri.Properties.AllowAccess = false;
                            suri.Properties.AllowAdd = false;
                            suri.Properties.AllowDelete = false;
                            suri.Properties.AllowEdit = false;
                            suri.Properties.AllowExport = false;
                            suri.Properties.AllowImport = false;
                            suri.Properties.AllowPrint = false;
                            suri.Properties.AllowSave = false;
                            suri.Save();
                        }
                    }
                    #endregion
                }
            }
            #endregion   
            #region Added
            if (m_dtFunctionSet.GetChanges(DataRowState.Added) != null)
            {
                foreach (DataRow drRule in m_dtFunctionSet.GetChanges(DataRowState.Added).Rows)
                {

                    #region Lưu Rule mới
                    System_RuleInfo sri = new System_RuleInfo(drRule["RuleID"].ToString());
                    sri.Properties.RuleName = drRule["RuleID"].ToString().Substring(3);
                    sri.Properties.RuleNameVi = drRule["RuleNameVi"].ToString();
                    sri.Properties.Level = System.Convert.ToInt16(drRule["Level"]);
                    sri.Properties.ParentID = drRule["ParentID"].ToString();
                    sri.Properties.Description = "";
                    sri.Properties.Owner = "Quản lý đào tạo";
                    sri.Save();
                    #endregion
                    #region Lưu Rule vào nhóm
                    System_Group_RuleInfo sgri = new System_Group_RuleInfo(m_sgi.Properties.System_GroupKey, sri.Properties.System_RuleKey);
                    sgri.Save();
                    #endregion
                    #region Lưu thông rule mới cho user
                    if (m_dtUserGroup != null)
                    {
                        foreach (DataRow dr in m_dtUserGroup.Rows)
                        {
                            System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(dr["System_UserKey"].ToString()), sgri.Properties.System_RuleKey);
                            suri.Properties.AllowAccess =  false;
                            suri.Properties.AllowAdd = false;
                            suri.Properties.AllowDelete = false;
                            suri.Properties.AllowEdit = false;
                            suri.Properties.AllowExport = false;
                            suri.Properties.AllowImport = false;
                            suri.Properties.AllowPrint = false;
                            suri.Properties.AllowSave = false;
                            suri.Save();
                        }
                    }
                    #endregion

                }
            }
            #endregion
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
           
            if (m_dtFunctionSet == null ||m_dtFunctionSet.Columns.Count==0)
            {
                m_dtFunctionSet = new DataTable();
               // m_dtFunctionSet.Columns.Add("System_RuleKey");
                m_dtFunctionSet.Columns.Add("RuleID");
                m_dtFunctionSet.Columns.Add("RuleName");
                m_dtFunctionSet.Columns.Add("RuleNameVi");
                m_dtFunctionSet.Columns.Add("Description");
                m_dtFunctionSet.Columns.Add("Level");
                m_dtFunctionSet.Columns.Add("ParentID");
                m_dtFunctionSet.Columns.Add("Owner");
                m_dtFunctionSet.AcceptChanges();
                
            }
            foreach (DevExpress.XtraTreeList.Nodes.TreeListNode nodeSource in tltFunctionNotSet.Selection)
            {
                //if (tltFunctionSet.FindNodeByFieldValue("RuleID", nodeSource["ParentID"]) == null)
                //{
                //    //DevExpress.XtraTreeList.Nodes.TreeListNode nodeParent = tltFunctionNotSet.FindNodeByFieldValue("RuleID", nodeSource["ParentID"]);
                //   /// AddParent(m_dtFunctionSet, nodeSource, tltFunctionSet);
                    
                //    //AddNode(nodeSource, tltFunctionSet, nodeParent);
                    
                //}
               
                //CopyDeep(m_dtFunctionSet, nodeSource, tltFunctionNotSet);
                AddNode(m_dtFunctionSet, tltFunctionSet, nodeSource,tltFunctionNotSet);
            }

            for (int i = 0; i < tltFunctionNotSet.Selection.Count; i++)
            {
                RemoveNode(tltFunctionNotSet, tltFunctionNotSet.Selection[i]);
            }

            tltFunctionSet.DataSource = m_dtFunctionSet;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtTarget"></param>
        /// <param name="nodeSource"></param>
        /// <param name="tltTarget"></param>
        private void CopyDeep(DataTable dtTarget, DevExpress.XtraTreeList.Nodes.TreeListNode nodeSource, DevExpress.XtraTreeList.TreeList tltTarget, DevExpress.XtraTreeList.TreeList tltSource)
        {
            if (nodeSource == null)
            {
                return;
            }
            if (tltTarget.FindNodeByFieldValue("RuleID", nodeSource["RuleID"]) == null)
            {
                dtTarget.Rows.Add(new object[] {
                    nodeSource["RuleID"],
                    nodeSource["RuleID"].ToString().Substring(3)
                    , nodeSource["RuleNameVi"],
                    "",
                    nodeSource["Level"],
                    nodeSource["ParentID"]});
            }

            foreach (DevExpress.XtraTreeList.Nodes.TreeListNode nodeChild in nodeSource.Nodes)
            {
                if (tltSource.FindNodeByFieldValue("ParentID", nodeSource["RuleID"]) != null)
                {
                    CopyDeep(dtTarget, nodeChild, tltTarget, tltSource);
                }
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtTarget"></param>
        /// <param name="nodeSource"></param>
        /// <param name="tltTarget"></param>
        private void AddParent(DataTable dtTarget, DevExpress.XtraTreeList.Nodes.TreeListNode nodeSource, DevExpress.XtraTreeList.TreeList tltTarget, DevExpress.XtraTreeList.TreeList tltSource)
        {

            DevExpress.XtraTreeList.Nodes.TreeListNode nodeParent = tltSource.FindNodeByFieldValue("RuleID", nodeSource["ParentID"]);

            if (nodeParent == null)
            { 
                return;
            }
            else
            {
                if (tltTarget.FindNodeByFieldValue("RuleID", nodeSource["ParentID"]) == null)
                {
                    AddParent(dtTarget, nodeParent, tltTarget, tltSource);
                   
                    dtTarget.Rows.Add(new object[] {
                       nodeParent["RuleID"],
               nodeParent["RuleID"].ToString().Substring(3), nodeParent["RuleNameVi"],
               "",nodeParent["Level"],nodeParent["ParentID"]});
                }
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="treeList"></param>
        /// <param name="node"></param>
        /// 
        private void AddNode(DataTable dtTarget, DevExpress.XtraTreeList.TreeList tltTarget, DevExpress.XtraTreeList.Nodes.TreeListNode node,DevExpress.XtraTreeList.TreeList tltSource)
        {
            DevExpress.XtraTreeList.Nodes.TreeListNode parentNode = node.ParentNode;
            //while (parentNode != null)
            //{
            //    if (tltTarget.FindNodeByFieldValue("RuleID", parentNode["RuleID"]) == null)
            //    {
            //        dtTarget.Rows.Add(new object[] {
            //           parentNode["RuleID"], 
            //           parentNode["RuleID"].ToString().Substring(3), 
            //           parentNode["RuleNameVi"],
            //           "",
            //           parentNode["Level"],
            //           parentNode["ParentID"]});
            //    }
            //    parentNode = parentNode.ParentNode;
            //}
            AddParent(dtTarget, node, tltTarget, tltSource);

            CopyDeep(dtTarget, node, tltTarget, tltSource);
        }

        private void RemoveNode(DevExpress.XtraTreeList.TreeList treeList, DevExpress.XtraTreeList.Nodes.TreeListNode node)
        {
            if (node == null)
            {
                return;
            }
            DevExpress.XtraTreeList.Nodes.TreeListNode nodeParent = node.ParentNode;
            if (node.Nodes.Count == 0)
            {
                treeList.Nodes.Remove(node);
            }
            else
            {
                for (int i = node.Nodes.Count - 1; i >= 0; i--)
                {
                    RemoveNode(treeList, node.Nodes[i]);
                    treeList.Nodes.Remove(node); 
                }
            }
            
            //if (nodeParent != null && nodeParent.Nodes.Count == 0)
            //{
            //    RemoveNode(treeList, nodeParent);
            //}
            //DataRow dRow = (from DataRow r in m_dtFunctionSet.Rows where r["RuleID"].ToString() == node["RuleID"].ToString() select r).FirstOrDefault();
            //m_dtFunctionSet.Rows.Remove(dRow);
            //treeList.Nodes.Remove(node);
            //try
            //{
            //    DevExpress.XtraTreeList.Nodes.TreeListNode nodeParent = node.ParentNode;
            //    for (int i = node.Nodes.Count - 1; i >= 0; i--)
            //    {
            //        RemoveNode(treeList, node.Nodes[i]);
            //    }

            //    if (treeList.Name == "tltFunctionSet")
            //    {

            //        //System_Group_RuleInfo sgri = new System_Group_RuleInfo(m_sgi.Properties.System_GroupKey, Guid.Parse(node["System_RuleKey"].ToString()));
            //        #region Xóa Rule khỏi User thuộc nhóm//

            //        //foreach (DataRow dr in m_dtUserGroup.Rows)
            //        //{
            //        //    System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(dr["System_UserKey"].ToString()), sgri.Properties.System_RuleKey);
            //        //    suri.Delete();
            //        //    suri.Save();
            //        //}

            //        #endregion


            //        DataRow dRow = (from DataRow r in m_dtFunctionSet.Rows where r["RuleID"].ToString() == node["RuleID"].ToString() select r).FirstOrDefault();
            //        m_dtFunctionSet.Rows.Remove(dRow);


            //        //sgri.Delete();
            //        //sgri.Save();
            //    }

            //    treeList.Nodes.Remove(node);

            if (nodeParent != null && nodeParent.Nodes.Count == 0)
            {
                RemoveNode(treeList, nodeParent);
            }
            //}
            //catch { }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            
            for (int i = 0; i < tltFunctionSet.Selection.Count; i++)
            {
                AddNode(m_dtFunctionNotSet, tltFunctionNotSet, tltFunctionSet.Selection[i],tltFunctionSet);

                DevExpress.XtraTreeList.Nodes.TreeListNode nodeParent = tltFunctionSet.Selection[i].ParentNode;

                RemoveNode(tltFunctionSet, tltFunctionSet.Selection[i]);

                //while (nodeParent != null)
                //{
                //    if (nodeParent.Nodes.Count == 0)
                //    {
                //        tltFunctionSet.Nodes.Remove(nodeParent);
                //    }
                //}
            }
    
        }

        private void btnSaveAndAdd_Click(object sender, EventArgs e)
        {
            if (txtGroupName.Text != "")
            {
                if (m_sgi == null)
                {
                    m_sgi = new System_GroupInfo(txtGroupName.Text.Trim());
                }
                else
                {
                    m_sgi.Properties.Name = txtGroupName.Text.Trim();
                }

                m_sgi.Properties.Description = meoDescription.Text;
                m_sgi.Save();
                SaveGroupRule();
                passData(m_sgi);
                txtGroupName.Text = "";
                meoDescription.Text = "";
               
            }
            else
            {
                MessageBox.Show("Tên nhóm không được rỗng");
            }
           
        }
        private void grcUserName_DoubleClick(object sender, EventArgs e)
        {
            if (grvSystem_User.GetFocusedRow() != null)
            {
                System_UserInfo sui = new System_UserInfo(grvSystem_User.GetFocusedRowCellValue("UserName").ToString());
                FrmCreateUser f = new FrmCreateUser(sui);
                //f.passData = new FrmCreateUser.PassData(DelegateUser);
                f.Text = "Sửa thông tin người dùng " + grvSystem_User.GetFocusedRowCellValue("UserName").ToString();
                f.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                f.ShowDialog();
            }
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            FrmUserList f = new FrmUserList();
            f.passData = new FrmUserList.PassData(DelegateUser);
            f.ShowDialog();
        }

        public void DelegateUser(DataTable dt)
        {
            LoadRuleByGroup();
            foreach (DataRow drUser in dt.Rows)
            {
                if (m_dtUserGroup.Columns.Count == 0)
                {
                    m_dtUserGroup.Columns.Add("System_UserKey");
                    m_dtUserGroup.Columns.Add("UserName");

                }

                DataRow drTemp = m_dtUserGroup.NewRow();
                drTemp["System_UserKey"] = drUser["System_UserKey"];
                drTemp["UserName"] = drUser["UserName"];

                m_dtUserGroup.Rows.Add(drTemp);

                System_User_GroupInfo sugi = new System_User_GroupInfo(Guid.Parse(drUser["System_UserKey"].ToString()), m_sgi.Properties.System_GroupKey);
                sugi.Save();

               #region Lưu quyền User
               
                foreach (DataRow dr in m_dtSys_User_Rule.Rows)
                {
                    System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(drUser["System_UserKey"].ToString()), Guid.Parse(dr["System_RuleKey"].ToString()));



                    if (dr["AllowAccess"] != DBNull.Value)
                        suri.Properties.AllowAccess = Convert.ToBoolean(dr["AllowAccess"]);
                    if (dr["AllowAdd"] != DBNull.Value)
                        suri.Properties.AllowAdd = Convert.ToBoolean(dr["AllowAdd"]);
                    if (dr["AllowDelete"] != DBNull.Value)
                        suri.Properties.AllowDelete = Convert.ToBoolean(dr["AllowDelete"]);
                    if (dr["AllowEdit"] != DBNull.Value)
                        suri.Properties.AllowEdit = Convert.ToBoolean(dr["AllowEdit"]);
                    if (dr["AllowExport"] != DBNull.Value)
                        suri.Properties.AllowExport = Convert.ToBoolean(dr["AllowExport"]);
                    if (dr["AllowImport"] != DBNull.Value)
                        suri.Properties.AllowImport = Convert.ToBoolean(dr["AllowImport"]);
                    if (dr["AllowPrint"] != DBNull.Value)
                        suri.Properties.AllowPrint = Convert.ToBoolean(dr["AllowPrint"]);
                    if (dr["AllowSave"] != DBNull.Value)
                        suri.Properties.AllowSave = Convert.ToBoolean(dr["AllowSave"]);

                    suri.Save();
                }

                #endregion
                grcSystem_User.DataSource = m_dtUserGroup;
            }
        }

        private void btnRemoveUser_Click(object sender, EventArgs e)
        {
            
            DialogResult dagResult = MessageBox.Show("Bạn thật sự muốn xóa thành viên"+grvSystem_User.GetFocusedRowCellValue("UserName").ToString()+ "khỏi nhóm", "Thông báo", MessageBoxButtons.OKCancel);
            if (dagResult == DialogResult.Cancel)
            {
                return;
            }
            try
            {
                //DataTable dtGroupRule = System_Group_RuleData.GetByGroup(m_sgi.Properties.System_GroupKey).ToDataTable();
                DataTable dtGroupRule = System_Group_RuleData.GetRuleForRemove(m_sgi.Properties.System_GroupKey, Guid.Parse(grvSystem_User.GetFocusedRowCellValue("System_UserKey").ToString())).ToDataTable();
                foreach (DataRow row in dtGroupRule.Rows)
                {
                    System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(grvSystem_User.GetFocusedRowCellValue("System_UserKey").ToString()), Guid.Parse(row["System_RuleKey"].ToString()));
                    suri.Delete();
                    suri.Save();
                }

                System_User_GroupInfo sugi = new System_User_GroupInfo(Guid.Parse(grvSystem_User.GetFocusedRowCellValue("System_UserKey").ToString()), m_sgi.Properties.System_GroupKey);
                sugi.Delete();
                sugi.Save();

                grvSystem_User.DeleteSelectedRows();
            }
            catch { }
        }

        private void FrmCreateGroup_Load(object sender, EventArgs e)
        {
            //DevExpress.XtraTreeList.Nodes.TreeListNode node = tltFunctionSet.FindNodeByFieldValue("RuleID", node["RuleID"]);
           
        }
        
    }
}